﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace GMusicBox.Core.Setting
{
    public class MainSetting
    {
        public static Settings Data { get; set; }
        public static string FileName { get { return Settings.SettingSavePath + "\\Config\\Configs\\Main.xml"; } }
        public static void Save() { Data.WriteAllSettings(FileName); }

        static string hash = "";

        public static void Update()
        {
            Data = new Settings(FileName);
            hash = File.ReadAllText(Settings.SettingSavePath + "\\hash", Encoding.Default);
        }

        public static string[] HashData
        {
            get
            {
                return hash.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            }
        }

        public static Point LastDesktopLyricLocation
        {
            get
            {
                string data = Data["LastDesktopLyricLocation"];
                if (data == "")
                    return new Point(Screen.PrimaryScreen.Bounds.Width / 4, Screen.PrimaryScreen.WorkingArea.Height - (new Font("幼圆", 25, FontStyle.Bold)).Height);
                else
                {
                    string[] s = data.Split(',');
                    return new Point(int.Parse(s[0]), int.Parse(s[1]));
                }
            }
            set { Data["LastDesktopLyricLocation"] = value.X.ToString() + "," + value.Y.ToString(); }
        }

        public static bool LastDesktopLyricLock
        {
            get
            {
                string data = Data["LastDesktopLyricLock"];
                if (data == "")
                    return false;
                else return bool.Parse(data);
            }
            set { Data["LastDesktopLyricLock"] = value.ToString(); }
        }

        public static int LastDesktopLyricWidth
        {
            get
            {
                string data = Data["LastDesktopLyricWidth"];
                if (data == "")
                    return Screen.PrimaryScreen.Bounds.Width / 2;
                else return int.Parse(data);
            }
            set { Data["LastDesktopLyricWidth"] = value.ToString(); }
        }

        public static bool LastDesktopLyricVisible
        {
            get
            {
                string data = Data["LastDesktopLyricVisible"];
                if (data == "")
                    return true;
                else return bool.Parse(data);
            }
            set { Data["LastDesktopLyricVisible"] = value.ToString(); }
        }

        public static bool LastDownloadLyric
        {
            get
            {
                string data = Data["LastDownloadLyric"];
                if (data == "")
                    return false;
                else return bool.Parse(data);
            }
            set { Data["LastDownloadLyric"] = value.ToString(); }
        }

        public static string LastDownloadNameRules
        {
            get
            {
                string data = Data["LastDownloadNameRules"];
                if (data == "")
                    return "${artist} - ${name}";
                else return data;
            }
            set { Data["LastDownloadNameRules"] = value; }
        }

        public static string LastDownloadSavePath
        {
            get { return Data["LastDownloadSavePath"]; }
            set { Data["LastDownloadSavePath"] = value; }
        }

        public static int[] LastPlay
        {
            get
            {
                string data = Data["LastPlay"];
                if (data == "")
                    return new int[] { -1, -1, -1 };
                else return Helper.String2IntArray(data);
            }
            set { Data["LastPlay"] = Helper.Array2String(value, ","); }
        }

        public static int[] LastSelected
        {
            get
            {
                string data = Data["LastSelected"];
                if (data == "")
                    return new int[] { 0, 0 };
                else return Helper.String2IntArray(data);
            }
            set { Data["LastSelected"] = Helper.Array2String(value, ","); }
        }

        public static int LastSelectedCloseEvent
        {
            get
            {
                string data = Data["LastSelectedCloseEvent"];
                if (data == "")
                    return 0;
                else return int.Parse(data);
            }
            set { Data["LastSelectedCloseEvent"] = value.ToString(); }
        }

        public static int LoopMode
        {
            get
            {
                string data = Data["LoopMode"];
                if (data == "")
                    return 1;
                else return int.Parse(data);
            }
            set { Data["LoopMode"] = value.ToString(); }
        }

        public static bool Mute
        {
            get
            {
                string data = Data["Mute"];
                if (data == "")
                    return false;
                else return bool.Parse(data);
            }
            set { Data["Mute"] = value.ToString(); }
        }

        public static double Volume
        {
            get
            {
                string data = Data["Volume"];
                if (data == "")
                    return 0.5;
                else
                {
                    double value = double.Parse(data);
                    if (value > 1) value /= 100;
                    return value;
                }
            }
            set { Data["Volume"] = value.ToString(); }
        }
    }
}
